package com.cmdb.system.service.impl;

import com.cmdb.common.core.domain.entity.SysUser;
import com.cmdb.common.utils.DateUtils;
import com.cmdb.common.utils.SecurityUtils;
import com.cmdb.common.utils.StringUtils;
import com.cmdb.system.domain.SysFiles;
import com.cmdb.system.domain.SysSms;
import com.cmdb.system.domain.SysSmsDeatil;
import com.cmdb.system.mapper.*;
import com.cmdb.system.service.ISysSmsService;
import com.cmdb.system.utils.SmsSenderUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * smsjobService业务层处理
 * 
 * @author cyq
 * @date 2025-06-14
 */
@Service
public class SysSmsServiceImpl implements ISysSmsService
{
    @Autowired
    private SysSmsMapper sysSmsMapper;

    @Autowired
    private SysUserMapper userMapper;
    @Autowired
    private SysSmsDeatilMapper sysSmsDeatilMapper;
    @Autowired
    private SysFileMapper smsFile;

    /**
     * 查询smsjob
     * 
     * @param id smsjob主键
     * @return smsjob
     */
    @Override
    public SysSms selectSysSmsById(Integer id)
    {
        SysSms sysSms = sysSmsMapper.selectSysSmsById(id);
        SysFiles files = new SysFiles();
        files.setDeptId(id);
        List<SysFiles> sysFiles = smsFile.selectSysFileList(files);
        sysSms.setFileList(sysFiles);
        return sysSms;
    }

    /**
     * 查询smsjob列表
     * 
     * @param sysSms smsjob
     * @return smsjob
     */
    @Override
    public List<SysSms> selectSysSmsList(SysSms sysSms)
    {
        List<SysSms> sysSms1 = sysSmsMapper.selectSysSmsList(sysSms);
        for (SysSms s : sysSms1){
            String deptId = s.getDeptId();
            boolean contains = deptId.contains(SecurityUtils.getDeptId() + "");
            if(contains){
                s.setFlag(1);
            }else {
                s.setFlag(0);
            }
            s.setFlag(1);
        }
        return sysSms1;
    }

    /**
     * 根据ID列表查询smsjob
     * 
     * @param ids ID列表
     * @return smsjob集合
     */
    @Override
    public List<SysSms> selectSysSmsByIds(List<Long> ids)
    {
        List<SysSms> sysSmsList = sysSmsMapper.selectSysSmsByIds(ids);
        for (SysSms s : sysSmsList) {
            String deptId = s.getDeptId();
            boolean contains = deptId.contains(SecurityUtils.getDeptId() + "");
            if(contains){
                s.setFlag(1);
            }else {
                s.setFlag(0);
            }
            s.setFlag(1);
        }
        return sysSmsList;
    }

    @Override
    public int selectSysSmsListJob(SysSms sysSms) {
        int count =0;
        List<SysSms> sysSms1 = sysSmsMapper.selectSysSmsList(sysSms);
        for (SysSms s : sysSms1){
            SysSmsDeatil   smsDeatil = new SysSmsDeatil();
            boolean contains = s.getDeptId().contains(SecurityUtils.getDeptId() + "");

            if (contains){
                smsDeatil.setDeptId(SecurityUtils.getDeptId());
                smsDeatil.setDataId(s.getId());
            List<SysSmsDeatil> sysSmsDeatils = sysSmsDeatilMapper.selectSysSmsDeatilList(smsDeatil);
            if(null==sysSmsDeatils||sysSmsDeatils.size()<=0){
                count++;
            }
            }
        }

        return count;
    }

    /**
     * 新增smsjob
     * 
     * @param sysSms smsjob
     * @return 结果
     */
    @Override
    @Transactional
    public int insertSysSms(SysSms sysSms)
    {
        sysSms.setCreateTime(DateUtils.getNowDate());
        sysSms.setStatus("Y");
        int i = sysSmsMapper.insertSysSms(sysSms);
        List<SysFiles> fileList = sysSms.getFileList();
        if(fileList.size()>0){
            for(SysFiles file:fileList){
                file.setStatus(0);
                file.setDeptId(sysSms.getId());
                file.setDataId(sysSms.getId());
                smsFile.insertSysFile(file);
            }

        }
        if(i>0){

            if(StringUtils.isNotEmpty(sysSms.getDeptId())){

                String[] ids = sysSms.getDeptId().split(",");

                List<SysUser> sysUsers = userMapper.SelectUserByDeptIds(ids);
                StringBuffer phones = new StringBuffer();
                for(SysUser user:sysUsers){
                    phones.append(user.getPhonenumber()).append("|");
                }
                SmsSenderUtil.sendNormalMessage(phones.toString(),"");
            }


        }
        return i;
    }

    /**
     * 修改smsjob
     * 
     * @param sysSms smsjob
     * @return 结果
     */
    @Override

    public int updateSysSms(SysSms sysSms)
    {
        sysSms.setUpdateTime(DateUtils.getNowDate());

        smsFile.deleteSysFileByDeptId(sysSms.getId());
        List<SysFiles> fileList = sysSms.getFileList();
        if(fileList.size()>0){
            for(SysFiles file:fileList){
                file.setStatus(0);
                file.setDeptId(sysSms.getId());
                file.setDataId(sysSms.getId());
                smsFile.insertSysFile(file);
            }

        }
        return sysSmsMapper.updateSysSms(sysSms);
    }

    /**
     * 批量删除smsjob
     * 
     * @param ids 需要删除的smsjob主键
     * @return 结果
     */
    @Override
    public int deleteSysSmsByIds(Long[] ids)
    {
        return sysSmsMapper.deleteSysSmsByIds(ids);
    }

    /**
     * 删除smsjob信息
     * 
     * @param id smsjob主键
     * @return 结果
     */
    @Override
    public int deleteSysSmsById(Long id)
    {
        return sysSmsMapper.deleteSysSmsById(id);
    }
}
